******************************************************************
** Zhirnov, Moral, Sedashov - Taking Distributions Seriously *****
** Replication Do File: Tables (January 20, 2022) **********
******************************************************************

clear all
set more off
set matsize 800
set seed 123456789

timer clear
timer on 1

ssc install moremata, replace
ssc install estout, replace

timer on 2

******************************************************************
****** Table 1 in Section 2 **************************************
******************************************************************
use "data/Robertson Teitelbaum 2011.dta", clear 
gen any_dispute = dispute > 0
gen l_flows_l = L.l_flows
gen polity2_l = L.polity2
eststo lo: logit any_dispute c.l_flows_l##c.polity2_l
 
clear
set obs 3
gen row=_n
gen polity2_l=-16 + 8*row
gen FDI1=10
gen FDI2=4000
reshape long FDI,i(row) j(j)
gen l_flows_l=log(FDI)
predict pr,pr
gen me = (e(b)[1,"any_dispute:l_flows_l"] + polity2_l*e(b)[1,"any_dispute:c.l_flows_l#c.polity2_l"])*pr*(1-pr)
gen me2 = me/FDI

replace l_flows_l=round(l_flows_l,0.1)
replace me=round(me,0.001)
replace me2=round(me2,0.00001)

lab var FDI "FDI Flow (million USD)"
lab var l_flows_l "ln(FDI Flow)"
lab var polity2_l "Polity"
lab var me "ME of ln(FDI Flow)"
lab var me2 "ME of FDI Flow" 
list FDI l_flows_l polity2_l me me2
* texsave FDI l_flows_l polity2_l me me2 using "Table1.tex", replace varlabels 

********************************************************
***************** Table 2 in Section 4 *****************
********************************************************
use "data/Robertson Teitelbaum 2011.dta", clear 
gen any_dispute = dispute > 0
gen polity2_l = L.polity2
gen growing = (l_flows+L.l_flows+L2.l_flows)>(L3.l_flows+L4.l_flows+L5.l_flows)
eststo lo: logit any_dispute i.growing##c.polity2_l

tabstat growing,by(polity2)

matrix beta=e(b)[.,e(depvar) + ":"]
matrix vcov=e(V)[e(depvar) + ":",e(depvar) + ":"]
drawnorm coef1-coef`=colsof(beta)', n(10000) means(beta) cov(vcov) clear
putmata coef=(*), replace

mata
polity2_l=range(-10,10,1)
nr=length(polity2_l)
X1=(J(nr,1,(0,1)),polity2_l,J(nr,1,0),polity2_l,J(nr,1,1))
X0=(J(nr,1,(1,0)),polity2_l,polity2_l,J(nr,1,(0,1)))
pr1=logistic(coef*X1')
pr0=logistic(coef*X0')
ef = pr1-pr0
e1 = mean(pr1)'
e0 = mean(pr0)'
means=mean(ef)'
ra=mm_quantile(ef, 1, (0.025 \ 0.975))'
res=(polity2_l,e0,e1,means,ra)
end
 
clear
getmata (z pr0 pr1 est lb ub)=res

replace pr1=round(pr1,0.001)
replace pr0=round(pr0,0.001)
replace est=round(est,0.001)
tostring lb, format(%9.3f) gen(lbt) force
tostring ub, format(%9.3f) gen(ubt) force
gen ci="(" + lbt + ", " + ubt + ")" 

lab var z "Polity"
lab var pr1 "Prob. of Strikes if FDI is Growing"
lab var pr0 "Prob. of Strikes if FDI is not Growing"
lab var est "Difference in Predictions"
lab var ci "Conf. Interval"
keep if inlist(z,-8,0,8)
list z pr0 pr1 est ci
* texsave z pr0 pr1 est ci using "Table2.tex", replace varlabels

timer off 1
timer off 2
timer list